package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class TAOSHOUYOUAttr implements AdapterAttr {


    public static void main(String[] args) {
        SalePartner salePartner = new SalePartner();
        salePartner.setSaleKey("3695992");
        salePartner.setOther("3f6abd4b0779e08806d5ba1fe6d419e4");
        String url = "https://www.taoshouyou.com/api/trades-union/getarea";
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> header = new HashMap<>();
        params.put("name","和平精英");
        data.put("data",JSONObject.toJSONString(params));
        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
        System.out.println(timestamp);
        String salt = "e0u6fnlag0";
        String cover1 = getCover(data);
//        cover1 = cover1.substring(0,cover1.length()-1);
        String cover = salt+timestamp+cover1+salePartner.getSaleKey()+salePartner.getOther();
        System.out.println(cover);
        String sign = DigestUtils.sha256Hex(cover);
        System.out.println(sign);
        header.put("AppId",salePartner.getSaleKey());
        header.put("AppKey",salePartner.getOther());
        header.put("timestamp",timestamp);
        header.put("sign",salt+sign);
        System.out.println(data);
        String response = HttpUtils.doPostFormData(url, data, header);
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String body = HttpRequest.get("https://www.taoshouyou.com/com/game/game2goodlist?_=1736316116780").execute().body();
        List<JSONObject> jsonObjects = JSONObject.parseArray(body, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            if(jsonObject.getString("goodsname").equals("账号")) {
                GamesRes gamesRes = new GamesRes();
                String id = jsonObject.getString("gameid");
                String name = jsonObject.getString("gamename");
                gamesRes.setId(id+"-"+name);
                gamesRes.setGameName(name);
                gamesResList.add(gamesRes);
            }
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String url = "https://www.taoshouyou.com/api/trades-union/getarea";
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> header = new HashMap<>();
        params.put("name",gameId.split("-")[1]);
        data.put("data",JSONObject.toJSONString(params));
        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
        System.out.println(timestamp);
        String salt = "e0u6fnlag0";
        String cover1 = getCover(data);
        cover1 = cover1.substring(0,cover1.length()-1);
        String cover = salt+timestamp+cover1+salePartner.getSaleKey()+salePartner.getOther();
        System.out.println(cover);
        String sign = DigestUtils.sha256Hex(cover);
        System.out.println(sign);
        header.put("AppId",salePartner.getSaleKey());
        header.put("AppKey",salePartner.getOther());
        header.put("timestamp",timestamp);
        header.put("sign","salt"+sign);
        String response = HttpUtils.doPostFormData(url, data, header);
        Object o = JSONArray.parseArray(response).get(0);
        JSONObject jsonObject = JSONObject.parseObject(o.toString());
        List<JSONObject> datas = JSONObject.parseArray(jsonObject.getString("client"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : datas) {
            InterServerRes interServer = new InterServerRes();
            interServer.setUuid(UUID.randomUUID().toString());
            interServer.setId(datum.getString("clientId"));
            interServer.setName(datum.getString("clientName"));
            String servers = datum.getString("serviceArea");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects2 = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects2) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.getString("id");
                    String title1 = object.getString("name");
                    interServer1.setUuid(UUID.randomUUID().toString());
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
//        String gameName = "";
//        String body = HttpRequest.get("https://www.taoshouyou.com/com/game/gamelist-interface").execute().body();
//        String result = MapUntils.unicodeToString(body);
//        JSONObject resJson = JSONObject.parseObject(result);
//        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
//        for(JSONObject jsonObject : jsonObjects){
//            if(jsonObject.getString("id").equals(gameId)){
//                gameName = jsonObject.getString("name");
//                break;
//            }
//        }
        String url = "https://www.taoshouyou.com/api/trades-union/getattr";
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> header = new HashMap<>();
        params.put("name",gameId.split("-")[1]);
        data.put("data",JSONObject.toJSONString(params));
        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
        System.out.println(timestamp);
        String salt = "e0u6fnlag0";
        String cover1 = MapUntils.getCover(data);
        cover1 = cover1.substring(0,cover1.length()-1);
        String cover = salt+timestamp+cover1+salePartner.getSaleKey()+salePartner.getOther();
        System.out.println(cover);
        String sign = DigestUtils.sha256Hex(cover);
        System.out.println(sign);
        header.put("AppId",salePartner.getSaleKey());
        header.put("AppKey",salePartner.getOther());
        header.put("timestamp",timestamp);
        header.put("sign",sign);
        String response = HttpUtils.doPostFormData(url, data, header);
        System.out.println(response);
        Object o1 = JSONArray.parseArray(response).get(0);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(o1.toString());
        List<JSONObject> data2 = JSONObject.parseArray(jsonObject.getString("attr"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data2.stream().filter(d -> d.getString("attrtype").equals("4")||d.getString("attrtype").equals("2")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data2.stream().filter(d -> d.getString("attrtype").equals("3")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data2.stream().filter(d -> d.getString("attrtype").equals("1")||d.getString("attrtype").equals("5")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UUID.randomUUID().toString());
            interAttrRes.setId(object.getString("id"));
            interAttrRes.setName(object.getString("name"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String val = object.getString("val");
            if(StringUtils.isNotEmpty(val)) {
                JSONArray jsonArray = JSONArray.parseArray(val);
                for (Object o : jsonArray) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUID.randomUUID().toString());
                    interAttrRes1.setId(o.toString());
                    interAttrRes1.setName(o.toString());
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }


    public static String getCover(Map<String,Object> params){


        log.info("通过Map.keySet遍历key和value：");

        StringBuffer resut = new StringBuffer();

        for (String key : params.keySet()) {

            log.info("key= "+ key + " and value= " + params.get(key));

            if (Objects.nonNull(params.get(key)) && StringUtils.isNotEmpty(String.valueOf(params.get(key)))){
                resut.append(params.get(key));
            }

        }
        return resut.toString();
    }
}